<?php
/**
 * Created by PhpStorm.
 * User: yuyan
 * Date: 2019/1/3
 * Time: 21:34
 */

namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Hook;
use app\shop\model\Order as OrderModel;
use app\shop\model\OrderList;
use app\shop\model\Address;
class Order extends Admin
{
    public function index()
    {
        $data_list = OrderList::order('status asc,id desc')->paginate();
        $arr = json_decode(json_encode($data_list),true);
        $orderIds = array_column($arr['data'],'order_id');
        $orderArr = OrderModel::where('id','in',$orderIds)->column('id,order_num');
        $status = [1=>'下单',2=>'支付成功',3=>'订单完成',4=>'删除订单'];
        return ZBuilder::make('table')
            ->setPageTitle('常规订单')
            ->setSearch(['id' => 'ID'])
            ->addColumns([
               // ['id', 'ID'],
                ['order_id', '订单编号',$orderArr],
                ['title', '商品名称'],
                ['price', '价格'],
                ['status', '状态',$status],
                ['create_time', '创建时间', 'datetime'],
                ['right_button', '操作', 'btn']
            ])
         //   ->addTopButtons('delete') // 批量添加顶部按钮
            ->addRightButtons('edit') // 批量添加右侧按钮
            ->addFilter('status', $status)
            ->addFilter('order_id', $orderArr)
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染页面
    }

    public function edit($id=null)
    {
        if ($id === null) $this->error('缺少参数');

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $orderData['status'] = $data['status'];
            $orderData['id'] = $data['order_list_id'];
            OrderModel::startTrans();
            $res = OrderModel::update($data);
            if ($res) {
                $res1 = OrderList::update($orderData);
                if($res1){
                    $user = OrderModel::get($data['id']);
                    Hook::listen('order_edit', $user);
                    OrderModel::commit();
                    $this->success('编辑成功', url('index'));
                }else{
                    OrderModel::rollback();
                    $this->error('编辑失败');
                }
            }else{
                OrderModel::rollback();
                $this->error('编辑失败');
            }
        }

        $order = OrderList::where('id',$id)->field('id,title,order_id')->find()->toArray();
        $orderInfo = OrderModel::where('id',$order['order_id'])->find()->toArray();
        $orderInfo['title'] = $order['title'];
        $orderInfo['order_list_id'] = $order['id'];
        $addr = Address::where('id',$orderInfo['address_id'])->find()->toArray();
        $orderInfo['address'] =$addr['country'].$addr['prov'].$addr['city'].$addr['district'].$orderInfo['add_address'];
        //配送方式
        $sendType = [1=>'普通快递',2=>'自提',3=>'物流',4=>'同城送货'];
        //作品包装
        $packType = [1=>'不要包装',2=>'木箱安全包装',3=>'蜂巢纸箱包装'];
        //支付方式
        $payType = [1=>'微信支付',2=>'余额支付',3=>'在线网银转账',4=>'对公银行转账'];
        //订单状态
        $status = [1=>'下单',2=>'支付成功',3=>'订单完成',4=>'删除订单'];
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden', 'id'],
                ['hidden', 'order_list_id'],
                ['static', 'order_num', '订单号'],
                ['static', 'title', '商品'],
                ['static', 'price', '价格'],
                ['text', 'real_price', '已扣款'],
                ['static', 'add_name', '收货人'],
                ['static', 'add_tel', '联系方式'],
                ['static', 'address', '收获地址'],
                ['select', 'send_type', '配送方式','',$sendType],
                ['select', 'pack_type', '作品包装','',$packType],
                ['select', 'pay_type', '支付方式','',$payType],
                ['textarea', 'remark', '备注'],
                ['radio', 'status', '状态', '',$status]
            ])
            ->setFormData($orderInfo) // 设置表单数据
            ->fetch();
    }
}